﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using Microsoft.Build.Framework;

namespace MSBuild.Mercurial
{
    /// <summary>
    /// Saves the current changes done on a Mercurial repository
    /// as a new Change Set.
    /// </summary>
    /// <remarks>
    /// This method use the <c>hg commit</c> command.
    /// </remarks>
    /// <example>
    /// Commit the changes done on the current MSBuild project:
    /// <code><![CDATA[
    /// <HgCommit
    ///     LocalPath="$(MSBuildProjectDirectory)"
    ///     Message="Update of the project"
    ///     Timeout="5000" />
    /// ]]></code>
    /// </example>
    [Command("commit")]
    public class HgCommit : AbstractHgTask
    {
        /// <summary>
        /// Initializes a new instance of the HgCommit class.
        /// </summary>
        public HgCommit()
        {
        }

        /// <summary>
        /// Gets or sets a value indicating whether the new and
        /// missing files should be automatically tagged as added
        /// or removed before committing.
        /// </summary>
        [Parameter("-A")]
        public bool AddRemove { get; set; }

        /// <summary>
        /// Gets or sets the inclusion pattern.
        /// </summary>
        [Parameter("-I")]
        public string Include { get; set; }

        /// <summary>
        /// Gets or sets the exclusion pattern.
        /// </summary>
        [Parameter("-X")]
        public string Exclude { get; set; }

        /// <summary>
        /// Gets or sets a specific commit date.
        /// </summary>
        [Parameter("-d")]
        public DateTime? Date { get; set; }

        /// <summary>
        /// Gets or sets the commit message.
        /// </summary>
        [Parameter("-m")]
        public string Message { get; set; }

        /// <summary>
        /// Gets or sets the file containing the commit message.
        /// </summary>
        [Parameter("-l")]
        public string LogFile { get; set; }

        /// <summary>
        /// Gets or sets the commit user.
        /// </summary>
        [Parameter("-u")]
        public string User { get; set; }

        /// <summary>
        /// Checks that one of the <see cref="HgCommit.Message"/> and <see cref="HgCommit.LogFile"/>
        /// properties is set before executing the task.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the task successfully executed; otherwise, <c>false</c>.
        /// </returns>
        public override bool Execute()
        {
            if (string.IsNullOrEmpty(this.Message) && string.IsNullOrEmpty(this.LogFile))
            {
                this.Log.LogError(Error.HgCommitMissingRequiredField);
                return false;
            }

            return base.Execute();
        }
    }
}
